package com.yang.cloud.controller;

import com.google.common.collect.Lists;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.yang.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yang.yang@karakal.com.cn
 * @version V1.0
 * @Description: 用户控制层
 * @date 2017-11-10 15:28
 */
@RestController
public class UserController {

    @Autowired
    private EurekaClient eurekaClient;//原生的netflix

    @Autowired
    private DiscoveryClient discoveryClient;//基於spring的

    @GetMapping("/user")
    public List<User> listAll() {
        ArrayList<User> list = Lists.newArrayList();
//        List<User> list = new ArrayList<>();
        User user = new User(1L, "zhangsan","7901");
        User user2 = new User(2L, "yangyang","7901");
        User user3 = new User(3L, "wangsong","7901");
        list.add(user);
        list.add(user2);
        list.add(user3);
        return list;
    }
    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id) {
        User user = new User(id, "zhangsan","7901");
        return user;
    }
    @PostMapping("/user")
    public User postUser(@RequestBody User user) {
        System.out.println("post");
        return user;
    }

    @GetMapping("/eureka-instance")
    public String serviceUrl() {
        InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
        return instance.getHomePageUrl();
    }
    @GetMapping("/instance-info")
    public ServiceInstance showInfo() {
        ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
        return localServiceInstance;
    }
}
